﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using clEntidad;

namespace clBD
{
    public class BDVehiculo:BDBase
    {
        public BDVehiculo(OleDbConnection cn):base(cn){ }
        public BDVehiculo(OleDbConnection cn, OleDbTransaction trx) : base(cn, trx) { }
        public OleDbDataReader get(String codigo)
        {
            String query = "select descripcion,llanta_rpto,escalera_grande,escalera_chica,ult_kilo, estado, ult_Id,placa,propio, " +
                "num_doc,num_serie1,num_serie2 " +
                "from mvehiculo where codigo ='" + codigo + "'";
            return execDr(query);
        }
        public void grabar(Vehiculo vehiculo)
        {
            String query = "select 1 from mvehiculo where codigo='" +
                vehiculo.codigo + "'";
            OleDbDataReader dr = execDr(query);
            if (!dr.HasRows)
            {
               
                //string estado = "I";
                //if (vehiculo.tipo.Equals ("C"))
                //{
                //    estado = "S";
                //}
                query = "insert into mvehiculo(codigo,placa,descripcion,llanta_rpto,escalera_grande," +
                    " escalera_chica,ult_kilo,id_local,ult_id, propio,num_doc,num_serie1,num_serie2, estado) "
                    + " values ('" + vehiculo.codigo + "','"+ vehiculo.placa + "','"
                    + vehiculo.marca.Trim().ToUpper() + "',"
                    + vehiculo.llantaRpto.ToString() + ","
                    + vehiculo.escaleraGrande.ToString() + ","
                    + vehiculo.escaleraChica.ToString() + ","

                    + vehiculo.ultimoKilometraje.ToString() + ","
                    + vehiculo.codLocal.ToString() + ","
                    + vehiculo.ultId.ToString() + ",'"
                    + vehiculo.tipo  + "','" +  vehiculo.numDoc + "','" + vehiculo.equipoIn1.numSerie + "','"
                    + vehiculo.equipoIn2.numSerie + "','"
                    + vehiculo.estado + "')";

            }
            else
            {
                query = "update mvehiculo set descripcion='" + vehiculo.marca.Trim().ToUpper()
                    + "',llanta_rpto =" + vehiculo.llantaRpto.ToString()
                    + ",escalera_grande=" + vehiculo.escaleraGrande.ToString()
                    + ",escalera_chica=" + vehiculo.escaleraChica.ToString()
                    + ",ult_kilo=" + vehiculo.ultimoKilometraje.ToString()
                    + ",ult_id=" + vehiculo.ultId.ToString()
                    + ",estado='" + vehiculo.estado + "'"
                    + ",num_doc='" + vehiculo.numDoc + "'"
                    + ",num_serie1='" + vehiculo.equipoIn1.numSerie + "'"
                    + ",num_serie2='" + vehiculo.equipoIn2.numSerie + "'"
                    + " where codigo='" + vehiculo.codigo + "'";
            }
            execute(query);

        }
        public void ingreso(string codigo, int kilometraje)
        {
            string update = "update mvehiculo set estado='I',ult_kilo=" + kilometraje.ToString() +
                " where placa='" + codigo + "'";
            execute(update);
        }
        public void salidaParticular(string codigo)
        {
            string update = "update mvehiculo set estado='S'" +
                " where codigo='" + codigo + "'";
            execute(update);
        }
    }
}
